package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//视为登录页面
@WebServlet(name = "LoginPageServlet", value = "/LoginPageServlet")
public class LoginPageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; charset=UTF-8");
        //获取登录操作转发的信息
        Object error = req.getAttribute("error");
        System.out.println("error = " + error);
        //将一个页面以相应数据返回给浏览器
        resp.getWriter().write("<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>登录</title>\n" +
                "</head>\n" +
                "<body bgcolor=\"white\">\n" +
                "<form action=" + req.getContextPath() + "/LoginServlet method=\"post\">\n" +
                "    <table align=\"center\" width=\"600px\" bgcolor=\"#f0f8ff\" height=\"320px\">\n" +
                "        <tr><th colspan=\"600px\" height=\"120px\" align=\"center\"><font size=\"5px\" color=\"black\">登录</font></th></tr>\n" +
                "        <tr><th colspan=\"600px\" height=\"60px\" align=\"center\"><font size=\"3px\" color=\"red\">" + (error == null ? "" : error) + "</font></th></tr>\n" +
                "        <tr><td colspan=\"600px\" height=\"50px\" align=\"center\"><input type=\"text\" name=\"account\" placeholder=\"请输入账号\"></td></tr>\n" +
                "        <tr><td colspan=\"600px\" height=\"50px\" align=\"center\"><input type=\"password\" name=\"password\" placeholder=\"请输入密码\"></td></tr>\n" +
                "        <tr><th width=\"260px\" height=\"40px\" align=\"right\"><input type=\"submit\" value=\"登录\"></th><th width=\"118px\" align=\"right\"><input type=\"reset\" value=\"重置\"></th><th></th><th></th></tr>\n" +
                "        <tr><th height=\"60px\"></th></tr>\n" +
                "    </table>\n" +
                "</form>\n" +
                "</body>\n" +
                "</html>");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
